package cn.zyn.eduservice.controller;


import cn.zyn.commonutils.R;
import cn.zyn.eduservice.entity.EduChapter;
import cn.zyn.eduservice.entity.chapter.ChapterVo;
import cn.zyn.eduservice.entity.vo.CourseInfoVo;
import cn.zyn.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @since 2022-05-29
 */
@Api(description="课程章节管理")
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;

    //1.根据课程id进行查询课程大纲列表
    @GetMapping("getChapterVideo/{courseid}")
    public R getChapterVideo(@PathVariable String courseid){
        List <ChapterVo> list =chapterService.getChapterVideoByCourseId(courseid);
        return R.ok().data("allChapterVideo",list);
    }

    //2.添加章节
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        chapterService.save(eduChapter);
        return R.ok();
    }
    //3.根据章节id查询_修改时数据回显
    @GetMapping("getChapterInfo/{chapterId}")
    public  R getChapterInfo(@PathVariable String chapterId){
        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("chapter",eduChapter);

    }
    //4.修改章节
    @PostMapping("updateChapter")
    public  R getChapterInfo(@RequestBody EduChapter eduChapter){
        chapterService.updateById(eduChapter);
        return R.ok();
    }
    /**
     *删除章节
     *有小节的章节不能删除
     */
    @DeleteMapping("{chapterId}")
    public  R deleteChapter(@PathVariable String chapterId){
        boolean flag = chapterService.deleteChapter(chapterId);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }


}

